package com.folderit.apps.rooms;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.folderit.apps.rooms.data.RoomsContract;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by César on 21/12/2014.
 */
public class ReservationsAdapter extends CursorAdapter {
    private static final int VIEW_TYPE_COUNT = 1;

    private Date mPreviousDate;

    /**
     * Cache of the children views for a room list item
     */
    public static class ViewHolder {
        public final TextView indexView;
        public final TextView fromToView;
        public final TextView descriptionView;
        public final TextView separatorView;

        public ViewHolder(View view) {
            indexView = (TextView) view.findViewById(R.id.list_item_reservation_index);
            fromToView = (TextView) view.findViewById(R.id.list_item_reservation_begin_end);
            descriptionView = (TextView) view.findViewById(R.id.list_item_reservation_description);
            separatorView = (TextView) view.findViewById(R.id.list_item_reservation_separator_textview);
        }
    }

    public ReservationsAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = LayoutInflater.from(context).inflate(R.layout.list_item_reservation, parent, false);

        ViewHolder viewHolder = new ViewHolder(view);
        view.setTag(viewHolder);

        return view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {

        ViewHolder viewHolder = (ViewHolder) view.getTag();

        // Read the beginning date from cursor and decide if the separator will be drawn
        String begin = cursor.getString(ReservationsFragment.COL_RESERVATION_BEGIN);
        boolean showSeparator = startNewGroup(cursor);

        if (showSeparator) {
            viewHolder.separatorView.setText(Utility.getFriendlyDayString(context, begin));
        }
        else {
            viewHolder.separatorView.setVisibility(View.GONE);
        }

        // Get current cursor position to set the index
        int index = cursor.getPosition() + 1;
        viewHolder.indexView.setText(Integer.toString(index));

        // Read end from cursor
        String end = cursor.getString(ReservationsFragment.COL_RESERVATION_END);

        // Set the text to view
        viewHolder.fromToView
                .setText(Utility.formatBeginEndDuration(context, begin, end));

        // Read description
        String description = cursor.getString(ReservationsFragment.COL_EVENT_TITLE);
        viewHolder.descriptionView.setText(description);
    }

    private boolean startNewGroup(Cursor cursor)
    {
        String begin = cursor.getString(ReservationsFragment.COL_RESERVATION_BEGIN);
        Date currentDate = RoomsContract.getDateTimeFromDb(begin);

        if (cursor.getPosition() == 0 || mPreviousDate == null) {
            mPreviousDate = currentDate;
            return true;
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String previousDateStr = dateFormat.format(mPreviousDate);
        String currentDateStr = dateFormat.format(currentDate);

        if (previousDateStr.equals(currentDateStr)) {
            return false;
        }

        mPreviousDate = currentDate;
        return true;
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return VIEW_TYPE_COUNT;
    }
}
